<template>
  <div class="updata-birthday">
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="onCancel"
      @confirm="onConfirm"
    />
  </div>
</template>

<script>
import dayjs from "dayjs";
import store from "@/store";
import { reRefrash_token } from "@/utils/setToken";
import { mapMutations } from "vuex";
import { updataUsername } from "@/api/user";

export default {
  name: "UpdataBirthday",
  components: {},
  props: {
    value: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      minDate: new Date(1950, 0, 0),
      maxDate: new Date(),
      currentDate: new Date(2000, 0, 1),
      birth: "",
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    ...mapMutations("user", ["setToken"]),
    async onConfirm(val) {
      console.log(dayjs(val).format("YYYY-MM-DD"));
      this.birth = dayjs(val).format("YYYY-MM-DD");
      this.$emit("input", this.birth);
      await updataUsername(
        store.getters.id,
        reRefrash_token({ user_birth: this.birth })
      );
      this.$toast.success("修改成功");
      this.setToken(reRefrash_token({ user_birth: this.birth }));
      this.$emit("close");
    },
    onCancel() {
      this.$emit("close");
    },
  },
};
</script>
<style lang='less' scoped>
</style>
